import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    redirect:'/login',
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {
        path: 'login',
        name: 'Login',
        component: () => import('../views/Login.vue'),
      },
    ],
  },
  {
    path: '/index',
    name: 'Index',
    component: () => import('../views/Index.vue'),
  },
  {
    redirect:'/error',
    path: '*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue'),
    children:[
      {
        path: 'error',
        name: 'error',
        component: () => import('../views/NotFound.vue'),
      },
    ],
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,form,next)=>{
console.log(to)
console.log(form)
if(to.name==='Login')return next()
if(sessionStorage.getItem('login')){
  return next()
}else{
  return next('/login')
}
})
export default router
